package com.mangosteen.interviewji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.exception.BusinessException;
import com.mangosteen.interviewji.mapper.QuestionAssociateMapper;
import com.mangosteen.interviewji.model.entity.QuestionAssociate;
import com.mangosteen.interviewji.model.entity.QuestionThumb;
import com.mangosteen.interviewji.model.vo.QuestionVO;
import com.mangosteen.interviewji.service.QuestionService;
import com.mangosteen.interviewji.service.QuestionThumbService;
import com.mangosteen.interviewji.mapper.QuestionThumbMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ASUS
* @description 针对表【question_thumb(题目点赞表)】的数据库操作Service实现
* @createDate 2025-11-10 16:46:36
*/
@Service
@Slf4j
public class QuestionThumbServiceImpl extends ServiceImpl<QuestionThumbMapper, QuestionThumb>
    implements QuestionThumbService{
    @Resource
    private QuestionAssociateMapper questionAssociateMapper;

    @Resource
    private QuestionService questionService;
    @Override
    @Transactional
    public Boolean submit(Long questionId, Long userId) {
        QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
        questionThumbQueryWrapper.eq("questionId",questionId);
        questionThumbQueryWrapper.eq("userId",userId);
        QuestionThumb questionThumb = this.getOne(questionThumbQueryWrapper);
        if (questionThumb!=null){
            //如果之前点赞过
            if (questionThumb.getIsCancel()==1){
                //点赞状态是1则是点赞操作
                doFavour(questionId,userId);
            }else {
                //状态是0则是取消点赞操作
                cancel(questionId,userId);
            }
        }else {
            //如果之前没有点赞过则一定是点赞操作
            doFavour(questionId,userId);
        }
        return true;
    }

    @Override
    public Boolean getStatus(Long questionId, Long userId) {
        QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
        questionThumbQueryWrapper.eq("questionId",questionId);
        questionThumbQueryWrapper.eq("userId",userId);
        QuestionThumb questionThumb = this.getOne(questionThumbQueryWrapper);
        if (questionThumb!=null){
            return questionThumb.getIsCancel()==0;
        }
        return false;
    }

    @Override
    public Page<QuestionVO> getUserThumb(Long current, Long size, Long id, HttpServletRequest request) {
        QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
        questionThumbQueryWrapper.eq("userId",id);
        List<QuestionThumb> questionThumbList = this.list(questionThumbQueryWrapper);
        List<Long> questionIdList = questionThumbList.stream().map(QuestionThumb::getQuestionId).collect(Collectors.toList());
        return questionService.getQuestionVOPage(current,size,questionIdList,request);
    }

    private Boolean cancel(Long questionId, Long userId) {
        log.info("用户"+userId+"取消点赞");
        QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
        questionThumbQueryWrapper.eq("questionId",questionId);
        questionThumbQueryWrapper.eq("userId",userId);
        QuestionThumb questionThumb = this.getOne(questionThumbQueryWrapper);
        questionThumb.setIsCancel(1);
        updateById(questionThumb);

        QueryWrapper<QuestionAssociate> questionAssociateQueryWrapper = new QueryWrapper<>();
        questionAssociateQueryWrapper.eq("questionId",questionId);
        QuestionAssociate questionAssociate = questionAssociateMapper.selectOne(questionAssociateQueryWrapper);
        if (questionAssociate==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"取消点赞失败");
        }else {
            questionAssociate.setFavourNum(questionAssociate.getFavourNum()-1);
            questionAssociateMapper.updateById(questionAssociate);
        }
        return true;
    }

    private Boolean doFavour(Long questionId, Long userId) {
        log.info("用户"+userId+"点赞");
        QueryWrapper<QuestionThumb> questionThumbQueryWrapper = new QueryWrapper<>();
        questionThumbQueryWrapper.eq("questionId",questionId);
        questionThumbQueryWrapper.eq("userId",userId);
        QuestionThumb questionThumb = this.getOne(questionThumbQueryWrapper);
        if (questionThumb!=null){
            questionThumb.setIsCancel(0);
            updateById(questionThumb);
        }else {
            questionThumb = new QuestionThumb();
            questionThumb.setQuestionId(questionId);
            questionThumb.setUserId(userId);
            questionThumb.setIsCancel(0);
            save(questionThumb);
        }
        QueryWrapper<QuestionAssociate> questionAssociateQueryWrapper = new QueryWrapper<>();
        questionAssociateQueryWrapper.eq("questionId",questionId);
        QuestionAssociate questionAssociate = questionAssociateMapper.selectOne(questionAssociateQueryWrapper);
        if (questionAssociate==null){
            questionAssociate = new QuestionAssociate();
            questionAssociate.setQuestionId(questionId);
            questionAssociate.setThumNum(1);
            questionAssociateMapper.insert(questionAssociate);
        }else {
            questionAssociate.setThumNum(questionAssociate.getFavourNum()+1);
            questionAssociateMapper.updateById(questionAssociate);
        }
        return true;
    }
}




